package com.supermap.dataHandling.controller;

import com.supermap.dataHandling.restFul.ResultEntity;
import com.supermap.dataHandling.restFul.ResultManger;
import com.supermap.dataHandling.service.EtlHandlingService;
import com.supermap.dataHandling.utils.FileTool;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * Created by bruce on 18/8/23 -- 17:21
 */
@RestController
@RequestMapping("/etl")
public class EtlController {
    @Autowired
    private EtlHandlingService etlHandlingService;

    /**
     * 3
     * 核查文件是否存在
     *
     * @param dir
     * @return
     */
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    public ResultEntity check(@RequestParam String dir) {
        //检查文件在不，不在就复制预制文件过去
        boolean status = etlHandlingService.checkFile(dir);
        etlHandlingService.moveEtl(dir);
        if (status) {
            FileTool.deleteZip(dir);
            //成功了就移动etl计算文件
            //etlHandlingService.moveEtl(dir);
            return ResultManger.success();
        } else {
            return ResultManger.error();
        }
    }
//4
    @RequestMapping(value = "/start", method = RequestMethod.GET)
    public ResultEntity start(@RequestParam String dir) {
        //重命名
        boolean step2 = etlHandlingService.renameAllFile(dir);
        if (step2) {
            //开始计算
            return ResultManger.success(etlHandlingService.startEtl(dir));
        }
        return ResultManger.error();
    }
//5
    @RequestMapping(value = "/downLoad", method = RequestMethod.GET)
    public ResultEntity downLoad(@RequestParam String dir, HttpServletResponse response) {
        File file = FileTool.getZip(dir);
        try {
            InputStream inputStream = new FileInputStream(file);
            OutputStream outputStream = response.getOutputStream();
            response.setContentType("application/x-download");
            response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            System.out.println("下载文件打包完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultManger.success();
    }

    /**
     * 2
     * 上传备案文件
     *
     * @param file    上传上来的备案文件
     * @param dirPath 目录路径
     * @return
     */
    @PostMapping("/uploadBack")
    public ResultEntity uploadBack(MultipartFile file, @RequestParam String dirPath) {
        //判断文件是否为空
        if (file.isEmpty()) {
            return ResultManger.error();
        }
        //加个时间戳，尽量避免文件名称重复
        String path = dirPath + File.separator + "备案.xlsx";
        File dest = new File(path);
        //判断文件是否已经存在
        /*
        if (dest.exists()) {
            return ResultManger.success("备案文件已存在");
        }*/

        try {
            file.transferTo(dest); //保存文件
        } catch (IOException e) {
            return ResultManger.error("备案文件上传失败" + e.getMessage());
        }
        return ResultManger.success("备案文件上传成功");
    }

    /**
     * 1
     * 获取目录下的文件夹列表
     *
     * @return
     */
    @RequestMapping(value = "/getFileList", method = RequestMethod.GET)
    public ResultEntity getFileList(@RequestParam String dir) {
        List<File> fileList = FileTool.getRootList(dir);
        return ResultManger.success(fileList);
    }
}
